<template>
    <div class="div-page">
        <div class="hint-txt">产品核心技术</div>
        <div class="hint-txt-2">PRODUCT CORE TECHNOLOGY</div>
        <div class="img-option">
            <div id="container">
                <div class="slide slide1"><span></span></div>
                <div class="slide slide2"><span></span></div>
                <div class="slide slide3"><span></span></div>
            </div>
        </div>
    </div>
    </div>
</template>
<script src="../utils/bannerScript.js"></script>
<script>
export default {
    name: 'Head',
    data() {
        return {

        }
    },
    mounted() {
  //容器对象
        window.addEventListener('resize', this.handleResize);
        this.initBanner()
        const width = window.innerWidth;
            console.log('窗口新宽度:', width);
            if(width<1080&&width>767){
                this.initBanner(320)
            }else if(width<767&&width>480){
                this.initBanner(100)

            }else if(width<480){
                this.initBanner(87)
            }else if(width>991){
                this.initBanner(320)
                
            }
    },
    beforeDestroy() {
         // 记得移除事件监听
        window.removeEventListener('resize', this.handleResize);
    },
    methods: {
        //
        handleResize(){
            location.reload();
            const width = window.innerWidth;
            if(width<1080&&width>767){
                this.initBanner(320)
            }else if(width<767&&width>480){
                this.initBanner(100)

            }else if(width<480){
                this.initBanner(87)
            }       
        },
        initBanner(val){
            const width = window.innerWidth;
            this.$nextTick(()=>{
            var box = document.getElementById('container');
            var slides = box.getElementsByClassName('slide');
            var slideWidth = slides[0].offsetWidth;
            var countWidth = slides[0].getElementsByTagName('span')[0].offsetWidth;
            var exposeWidth =val;
            var borderColorOpacity = 'rgba(255,255,255,.5)';
            var borderColorWhite = '#ffffff';
            var exposeScale = '1.2';
            var currentScale = '1.6';
        // 设置容器宽度
        var boxWidth = slideWidth + (slides.length - 1) * exposeWidth;
        box.style.width = boxWidth + 'px';

        // 设置每个滑块的初始样式
        function setSlidePos() {
            for (var i = 1; i < slides.length; i++) {
            slides[i].style.left = slideWidth + exposeWidth * (i - 1) + 'px';
            }
            for (var i = 0; i < slides.length; i++) {
            var spanDistance, spanScale, spanColor;
            if (i < 1) {
                spanDistance = (slideWidth - countWidth) / 2 + 'px';
                spanScale = currentScale;
                spanColor = borderColorWhite;
            } else {
                spanDistance = (exposeWidth - countWidth) / 2 + 'px';
                spanScale = exposeScale;
                spanColor = borderColorOpacity;
            }
            slides[i].getElementsByTagName('span')[0].style.left = spanDistance;
            slides[i].getElementsByTagName('span')[0].style.transform = 'scale(' + spanScale + ')';
            slides[i].getElementsByTagName('span')[0].style.borderColor = '' + spanColor + '';
            }
  }
  setSlidePos();

  // 移动距离
  var translate = slideWidth - exposeWidth;

  // 为每个滑块绑定事件
  for (var i = 0; i < slides.length; i++) {
    (function(i) {
      slides[i].onmouseover = function() {
        console.log('1======为每个滑块绑定事件=====')
        setSlidePos();
        for (var j = 1; j <= i; j++) {
          var exposeSpan = slides[j - 1].getElementsByTagName('span')[0]
          slides[j].style.left = parseInt(slides[j].style.left, 10) - translate + 'px';
          exposeSpan.style.left = (exposeWidth - countWidth) / 2 + 'px';
          exposeSpan.style.transform = 'scale(' + exposeScale + ')';
          exposeSpan.style.borderColor = borderColorOpacity;
        }
        var currentSpan = slides[i].getElementsByTagName('span')[0];
        currentSpan.style.left = (slideWidth - countWidth) / 2 + 'px';
        currentSpan.style.transform = 'scale(' + currentScale + ')';
        currentSpan.style.borderColor = borderColorWhite;
      };
    })(i);
  }
        })
        }
    }
}
</script>
<style scoped="scoped" lang="scss">
.div-page {
    background: #fff;
}

.margin-left-right {
    padding-left: 8.5vw;
    padding-right: 8.5vw;
}

.hint-txt {
    text-align: center;
    padding-top: 70px;
    font-size: 42px;
    font-weight: 700;
    color: rgba(0, 0, 0, 1);
}

.hint-txt-2 {
    text-align: center;
    margin-top: 12px;
    font-size: 24px;
    font-weight: 400;
    letter-spacing: 8px;
    color: rgba(102, 102, 102, 1);
}

.img-option {
    margin-top: 83px;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media screen and (max-width: 1080px) {
    .margin-left-right {
        padding-left: 0.3vw;
        padding-right: 0.3vw;
    }

    .hint-txt {
        text-align: center;
        padding-top: 60px;
        font-size: 32px;
        font-weight: 700;
        color: rgba(0, 0, 0, 1);
    }

    .hint-txt-2 {
        text-align: center;
        margin-top: 12px;
        font-size: 18px;
        font-weight: 400;
        letter-spacing: 4px;
        color: rgba(102, 102, 102, 1);
    }

    .img-option {
        margin-top: 63px;
        display: flex;
        align-items: center;
    }

    #container {
        position: relative;
        height: 203px !important;
        overflow: hidden;
        border-radius: 2px;
    }

    .slide {
        position: absolute;
        display: block;
        left: 0;
        width: 30vw;
        transition: all .4s ease;
        cursor: pointer;
        height: 203px !important;
    }

    .slide1 {
        background-image: url('~@/assets/pc/core3.png');
        background-repeat: no-repeat;
        background-size: 100% 203px !important;

    }

    .slide2 {
        background-image: url('~@/assets/pc/core1.png');
        background-repeat: no-repeat;
        background-size: 100% 203px !important;

    }

    .slide3 {
        background-image: url('~@/assets/pc/core2.png');
        background-repeat: no-repeat;
        background-size: 100% 203px !important;

    }

}

@media screen and (max-width: 767px) {

    /* 样式规则 */
    .margin-left-right {
        padding-left: 0.1vw;
        padding-right: 0.1vw;
    }

    .hint-txt {
        text-align: center;
        padding-top: 60px;
        font-size: 32px;
        font-weight: 700;
        color: rgba(0, 0, 0, 1);
    }

    .hint-txt-2 {
        text-align: center;
        margin-top: 12px;
        font-size: 18px;
        font-weight: 400;
        letter-spacing: 4px;
        color: rgba(102, 102, 102, 1);
    }

    .img-option {
        margin-top: 63px;
        display: flex;
        align-items: center;
    }

    #container {
        position: relative;
        height: 123px !important;
        overflow: hidden;
        border-radius: 2px;
    }

    .slide {
        position: absolute;
        display: block;
        left: 0;
        width: 30vw;
        transition: all .4s ease;
        cursor: pointer;
        height: 123px !important;
    }

    .slide1 {
        background-image: url('~@/assets/pc/core3.png');
        background-repeat: no-repeat;
        background-size: 100% 123px !important;

    }

    .slide2 {
        background-image: url('~@/assets/pc/core1.png');
        background-repeat: no-repeat;
        background-size: 100% 123px !important;

    }

    .slide3 {
        background-image: url('~@/assets/pc/core2.png');
        background-repeat: no-repeat;
        background-size: 100% 123px !important;

    }

    span {
        color: white;
        text-align: center;
        display: block;
        position: absolute;
        width: 40px;
        height: 40px;
        line-height: 44px;
        border-radius: 50%;
        top: 50%;
        margin-top: -20px;
        font-size: 40px;
        transition: all .4s ease;
    }
}

@media screen and (max-width: 480px) {
    .margin-left-right {
        padding-left: 24px;
        padding-right: 24px;
    }


    .hint-txt {
        text-align: center;
        padding-top: 60px;
        font-size: 32px;
        font-weight: 700;
        color: rgba(0, 0, 0, 1);
    }

    .hint-txt-2 {
        text-align: center;
        margin-top: 12px;
        font-size: 12px;
        font-weight: 400;
        letter-spacing: 4px;
        color: rgba(102, 102, 102, 1);
    }

    .img-option {
        margin-top: 63px;
        display: flex;
        align-items: center;
    }


    #container {
        width: 100vw;
        position: relative;
        height: 143px !important;
        overflow: hidden;
        border-radius: 2px;
    }

    .slide {
        position: absolute;
        display: block;
        left: 0;
        width: 40vw;
        transition: all .4s ease;
        cursor: pointer;
        height: 143px !important;
    }

    .slide1 {
        background-image: url('~@/assets/pc/coreThree.png');
        background-repeat: no-repeat;
        background-size: 100% 143px !important;
    }

    .slide2 {
        background-image: url('~@/assets/pc/coreTwo.png');
        background-repeat: no-repeat;
        background-size: 100% 143px !important;
    }

    .slide3 {
        background-image: url('~@/assets/pc/coreOne.png');
        background-repeat: no-repeat;
        background-size: 100% 143px !important;
    }

    span {
        color: white;
        text-align: center;
        display: block;
        position: absolute;
        width: 40px;
        height: 40px;
        line-height: 44px;
        border-radius: 50%;
        top: 50%;
        margin-top: -20px;
        font-size: 40px;
        transition: all .4s ease;
    }
}


#container {
    position: relative;
    height: 473px;
    overflow: hidden;
    border-radius: 2px;
}

.slide {
    position: absolute;
    display: block;
    left: 0;
    width: 40vw;
    transition: all .4s ease;
    cursor: pointer;
    height: 473px;
}

.slide1 {
    background-image: url('~@/assets/pc/core3.png');
    background-repeat: no-repeat;
    background-size: 100% 473px;

}

.slide2 {
    background-image: url('~@/assets/pc/core1.png');
    background-repeat: no-repeat;
    background-size: 100% 473px;

}

.slide3 {
    background-image: url('~@/assets/pc/core2.png');
    background-repeat: no-repeat;
    background-size: 100% 473px;

}

span {
    color: white;
    text-align: center;
    display: block;
    position: absolute;
    width: 40px;
    height: 40px;
    line-height: 44px;
    border-radius: 50%;
    top: 50%;
    margin-top: -20px;
    font-size: 40px;
    transition: all .4s ease;
}
</style>
